<?php

namespace app\api\controller\shop;

use app\common\controller\Api;
use app\common\model\Category as ModelCategory;
use app\common\model\CategoryInfo;
use app\common\model\Shop;
use app\common\model\shop\Goods as GoodsModel;
use app\common\model\shop\goods\Category as GoodsCategory;

/**
 * 商品分类
 * @ApiWeigh (97) 
 */
class Category extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['list'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    // protected $auth = ['test3'];


    /**
     * 商品分类列表
     * @Author xy
     * @ApiTitle 
     * @ApiMethod (POST)
     * @ApiParams (name="pid", type="integer", required=true, sample="0=一级分类")
     * @ApiReturnParams (name="code", type="integer", required=true, sample="0")
     * @ApiReturn ({'code':'0','msg':'返回成功'})
     */
    public function list()
    {
        $params = request()->param();
        extract($params);
        $acct_id = $this->auth->acct_id;
        $model = new ModelCategory;
        if (isset($pid) && $pid > 0) {
            $category_ids = CategoryInfo::where('acct_id', $acct_id)->column('category_id');
            $model->where('pid', $pid);
            $model->whereIn('id', $category_ids);
        } else {
            $category_ids = CategoryInfo::where('acct_id', $acct_id)->group('category_id')->column('category_id');
            $pids = ModelCategory::where('id', 'in', $category_ids)->where('type', 'goods')->where('pid', 0)->column('pid');
            $model->whereIn('pid', $pids);
        }
        $res = $model->where('type', 'goods')
            ->where('status', 'normal')
            ->order('weigh desc,id desc')
            ->select();
        $this->responseMsg(0, $res);
    }
}
